<?php 
class typeAction extends action{
	public function index(){
		I('type');
		$oCategory = new type();
		$data = $oCategory->get_list(0, 20);
		$GLOBALS['smarty']->assign('data', $data);
		$GLOBALS['smarty']->display('type.html');
	}
	
	public function add(){
		$submit = !isset($_POST['submit'])?null:$_POST['submit'];
		if($submit){
			I('type');
			$oCategory = new type();
			$aRg['name'] = $_POST['name'];
			$aRg['keywords'] = $_POST['keywords'];
			$aRg['parent_id'] = $_POST['parent'];
			$aRg['enabled'] = $_POST['enabled'];
			$aRg['brief'] = $_POST['brief'];
			$aRg['sort'] = isset($_POST['sort'])?$_POST['sort']:1;
			$aRg['is_hot'] = !empty($_POST['is_hot'])?1:0;
			$aRg['is_best'] = !empty($_POST['is_best'])?1:0;
			$aRg['page_title'] = !empty($_POST['page_title'])?1:0;
			$aRg['url'] = !empty($_POST['url'])?1:0;
			$aRg['add_time'] = time();
			$res = $oCategory->add($aRg);
			if(!$res){
				$this->makeError('添加失败！');
			}
			else{
				$this->direct('type');
			}
		}
		else{
			$GLOBALS['smarty']->assign('act', 'add');
			$GLOBALS['smarty']->display('type.html');
		}
	}
	
	public function update(){
		$submit = !isset($_POST['submit'])?null:$_POST['submit'];
		if($submit){
			I('type');
			$oCategory = new type();
			$typeId = !empty($_POST['id'])?$_POST['id']:null;
			if(!$typeId){
				$this->makeError('非法访问！');
			}
			
			$aRg['name'] = $_POST['name'];
			$aRg['keywords'] = $_POST['keywords'];
			$aRg['parent_id'] = $_POST['parent'];
			$aRg['enabled'] = $_POST['enabled'];
			$aRg['brief'] = $_POST['brief'];
			$aRg['sort'] = isset($_POST['sort'])?$_POST['sort']:1;
			$aRg['is_hot'] = !empty($_POST['is_hot'])?1:0;
			$aRg['is_best'] = !empty($_POST['is_best'])?1:0;
			$aRg['page_title'] = !empty($_POST['page_title'])?$_POST['page_title']:'';
			$aRg['url'] = !empty($_POST['url'])?$_POST['url']:'';
			$res = $oCategory->add($aRg, ' WHERE type_id = '.$typeId);
			if(!$res){
				$this->makeError('更新失败！');
			}
			else{
				$this->direct('type');
			}
		}
		else{
			$typeId = isset($_GET['id'])?$_GET['id']:null;
			if(!$typeId){
				$this->makeError('非法访问！');
			}
			I('type');
			$oCategory = new type();
			$row = $oCategory->get_info($typeId);
			$GLOBALS['smarty']->assign('act', 'update');
			$GLOBALS['smarty']->assign('row', $row);
			$GLOBALS['smarty']->display('type.html');
		}		
	}
	
	public function delete(){
		$typeId = isset($_GET['id'])?$_GET['id']:null;
		if(!$typeId){
			$this->makeError('非法访问！');
		}
		I('type');
		$oCategory = new type();
		$oCategory->del($typeId);
	}
}
?>